package bb.lanxing.fragment.routebook;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.umeng.analytics.MobclickAgent;

import bb.lanxing.R;
import bb.lanxing.adapter.SimpleViewPagerAdapter;
import bb.lanxing.common.config.UmengEventConst;


public class MyRouteBookFragment extends Fragment {
    private SimpleViewPagerAdapter adapter;
    private int position;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        return layoutInflater.inflate(R.layout.fragment_lushu_my, viewGroup, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle bundle) {
        super.onViewCreated(view, bundle);
        MobclickAgent.onEventValue(getContext(), UmengEventConst.LUSHU_MINE, null, 1);
        final TabLayout tabLayout = view.findViewById(R.id.tab_lushu_nav);
        final ViewPager viewPager = view.findViewById(R.id.pager_lushu_nav_container);
        this.adapter = new SimpleViewPagerAdapter(getContext(), getChildFragmentManager());
        Bundle bundle2 = new Bundle(getArguments());
        this.adapter.addItem(getString(R.string.str_lushu_downloaded), RouteBookLocalListFragment.class, bundle2);
        Bundle bundle3 = new Bundle(getArguments());
        this.adapter.addItem(getString(R.string.str_lushu_created_by_myself), MyRouteBookListFragment.class, bundle3);
        Bundle bundle4 = new Bundle(getArguments());
        this.adapter.addItem(getString(R.string.str_lushu_favorite), CollectionRouteBookListFragment.class, bundle4);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int i) {
            }

            @Override
            public void onPageScrolled(int i, float f, int i2) {
            }

            @Override
            public void onPageSelected(int i) {
                MyRouteBookFragment.this.position = i;
            }
        });
        viewPager.setAdapter(this.adapter);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(adapter.getCount() - 1);
    }

    public void refreshItem() {
        if (adapter != null) {
            ((RouteBookListFragmentNew) adapter.getItem(this.position)).refresh();
        }
    }
}
